package kau.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import kau.dao.CustomerDAOImpl;
import kau.dao.RegisterationDAOImpl;
import kau.exception.DuplicationException;
import kau.exception.RecordNotFoundException;
import kau.vo.Customer;
import kau.vo.Registeration;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class CustomerAction extends MultiActionController {
	public ModelAndView input(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("customer action input entered!!!!");
		ModelAndView nextPage = new ModelAndView();
		nextPage.setViewName("write.jsp");

		return nextPage;
	}

	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		
		String custId=request.getParameter("inputCustId");
		String password=request.getParameter("inputPassword");
		String custName=request.getParameter("custName");
		
		String jumin1=request.getParameter("jumin1");
		String jumin2=request.getParameter("jumin2");
		String jumin=jumin1+jumin2;
		
		
		String custEmail=request.getParameter("custEmail");
		String email=request.getParameter("email");
		String allEmail=custEmail+"@"+email;
		
		
		String phoneNumber1=request.getParameter("phoneNumber1");
		String phoneNumber2=request.getParameter("phoneNumber2");
		String phoneNumber3=request.getParameter("phoneNumber3");
		String phoneNumber=phoneNumber1+phoneNumber2+phoneNumber3;
		
		String parentPhoneNumber1=request.getParameter("parentPhoneNumber1");
		String parentPhoneNumber2=request.getParameter("parentPhoneNumber2");
		String parentPhoneNumber3=request.getParameter("parentPhoneNumber3");
		String parentPhoneNumber=parentPhoneNumber1+parentPhoneNumber2+parentPhoneNumber3;
		
		String passwordQuestion=request.getParameter("passwordQuestion");
		String passwordAnswer=request.getParameter("passwordAnswer");
		
		
		Customer customer = new Customer();
		customer.setPassword(password);
		customer.setCustId(custId);
		customer.setCustName(custName);
		customer.setCustSsn(jumin);
		customer.setCustEmail(allEmail);
		customer.setPhoneNumber(phoneNumber);
		customer.setParentPhoneNumber(parentPhoneNumber);
		customer.setPasswordQuestion(passwordQuestion);
		customer.setPasswordAnswer(passwordAnswer);
		
		customer.setCustLevel(4);
		customer.setJoinDate("");
		
		//System.out.println(customer);
		
		
		
		
		
		
		try {
			CustomerDAOImpl.getInstance().insert(customer);
			request.setAttribute("message", "등록성공!!");
		} catch (DuplicationException e) {
			request.setAttribute("message", e.getMessage());
		}

		nextPage.setViewName("/index.jsp");

		return nextPage;
	}

	public ModelAndView lookForCustId(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		String custId = null;
		String custSsn = request.getParameter("jumin1")
				+ request.getParameter("jumin2");
		try {
			custId = CustomerDAOImpl.getInstance().lookForCustId(
					request.getParameter("custName"), custSsn);

			//System.out.println("아이디찾기결과:custId=" + custId);
		} catch (RecordNotFoundException e) {
			System.out.println(e.getMessage());
		}
		if (custId == null) {
			request.setAttribute("message", "해당정보가 없습니다");
			nextPage.setViewName("index.jsp?content=/cust/idNotFounded.jsp");
		} else {
			nextPage.setViewName("index.jsp?content=/cust/searchedId.jsp?custId="
					+ custId);
			// request.setAttribute("custId", custId);
		}
		return nextPage;
	}

	public ModelAndView showId(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		nextPage.setViewName("index.jsp?content=cust/searchedId.jsp");

		return nextPage;
	}

	public ModelAndView searchPass(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		nextPage.setViewName("index.jsp?content=cust/inputIdSsn.jsp");

		return nextPage;
	}

	public ModelAndView passSearchInput(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		nextPage.setViewName("index.jsp?content=cust/question.jsp");

		return nextPage;
	}

	public ModelAndView lookForPassword(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();

		//System.out.println(request.getParameter("cust"));
		//System.out.println(request.getParameter("passwordQuestion"));
		//System.out.println(request.getParameter("passwordAnswer"));

		String password = null;
		try {
			password = CustomerDAOImpl.getInstance().lookForPassword(
					request.getParameter("cust"),
					request.getParameter("passwordQuestion"),
					request.getParameter("passwordAnswer"));

			//System.out.println("패스워드찾기결과:password=" + password);
		} catch (RecordNotFoundException e) {
			System.out.println(e.getMessage());
		}
		if (password == null) {
			request.setAttribute("message", "해당정보가 존재하지 않습니다");
			nextPage.setViewName("index.jsp?content=/cust/passwordNotFounded.jsp");
		} else {
			nextPage.setViewName("index.jsp?content=/cust/searchedPassword.jsp?password="
					+ password);
			// nextPage.setViewName("index.jsp?content=/cust/searchedPassword.jsp");
			// request.setAttribute("password", "password");
		}

		return nextPage;
	}

	public ModelAndView update_cust(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		//System.out.println("update_cust");
		String custEmail = request.getParameter("mailAccount") + "@"
				+ request.getParameter("mailDomain");
		String phoneNumber = request.getParameter("phoneNumber1")+request.getParameter("phoneNumber2")+request.getParameter("phoneNumber3");

		
		
		try {
			CustomerDAOImpl.getInstance().update_cust(
					request.getParameter("custId"), custEmail,phoneNumber,
					request.getParameter("passwordQuestion"),
					request.getParameter("passwordAnswer"));

			//System.out.println("수정성공");
		} catch (RecordNotFoundException e) {
			System.out.println(e.getMessage());
		}
		nextPage.setViewName("index.jsp?contentMenu=/cust/myPage.jsp&content=/cust/mainPage.jsp");

		return nextPage;
	}

	public ModelAndView update_pass(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();

		try {
			CustomerDAOImpl.getInstance().update_pass(
					request.getParameter("custId"),
					request.getParameter("password1"));
			/*
			 * System.out.println(request.getParameter("custId")+
			 * request.getParameter("password1"));
			 */
			System.out.println("패스워드변경성공");
		} catch (RecordNotFoundException e) {
			System.out.println(e.getMessage());
		}
		request.setAttribute("message", "패스워드변경완료!!! 잠시후 홈으로 이동합니다.");
		nextPage.setViewName("index.jsp?contentMenu=/cust/myPage.jsp&content=/cust/mainPage.jsp");

		return nextPage;
	}

	public ModelAndView update1(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		nextPage.setViewName("index.jsp?content=cust/update1.jsp");

		return nextPage;
	}

	public ModelAndView update2(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		nextPage.setViewName("index.jsp?content=cust/myPage.jsp");

		return nextPage;
	}

	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		String custId = request.getParameter("custId");
		String password = request.getParameter("password");
		
		//System.out.println(custId + " " + password);
		try {
			CustomerDAOImpl.getInstance().delete(custId, password);
			request.setAttribute("message", "삭제성공!!");
		} catch (RecordNotFoundException e) {
			request.setAttribute("message", e.getMessage());
		}
		
		ArrayList<Customer> custList = CustomerDAOImpl.getInstance()
				.getAllCustomers();

		//System.out.println(custList.size());
		request.setAttribute("custList", custList);

		nextPage.setViewName("index.jsp?contentMenu=/admin/menuPage.jsp&content=/admin/custList.jsp");
		return nextPage;
	}
	
	public ModelAndView delete_self(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		String custId = request.getParameter("custId");
		String password = request.getParameter("password");
		
		System.out.println(custId + " " + password);
		try {
			CustomerDAOImpl.getInstance().delete(custId, password);
			request.setAttribute("message", "삭제성공!!");
			HttpSession session = request.getSession();
			session.removeAttribute("custId");
		} catch (RecordNotFoundException e) {
			request.setAttribute("message", e.getMessage());
		}
		
		
		nextPage.setViewName("index.jsp");
		return nextPage;
	}

	public ModelAndView getCustomer(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		
		String selectedId = request.getParameter("selectedId");
	
		//System.out.println("admin"+selectedId);
		Customer customer = CustomerDAOImpl.getInstance().getCustomer(selectedId);
		request.setAttribute("customer", customer);
		nextPage.setViewName("index.jsp?contentMenu=/admin/menuPage.jsp&content=/admin/custInfo.jsp");

		return nextPage;
	}
public ModelAndView getAllCustomer(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		ArrayList<Customer> custList = CustomerDAOImpl.getInstance().getAllCustomers();
	/*
		System.out.println("-----------"+request.getParameter("nextType"));
		request.setAttribute("custList", custList);
		
		String nextType = request.getParameter("nextType");
		System.out.println("nextType:"+nextType);
		if (nextType.equals("admincustList")) {
			nextPage.setViewName("/admin/custList.jsp");
		} else if (nextType.equals("custDelete")) {
			nextPage.setViewName("/admin/custDelete.jsp");
		}*/
		request.setAttribute("custList", custList);
		nextPage.setViewName("/admin/custList.jsp");
		return nextPage;
	}

	public ModelAndView loginCheck(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		// nextPage.setViewName("index.jsp");

		String custId = request.getParameter("custId");
		String password = request.getParameter("password");
		
	//	System.out.println("custId:"+custId);
		//System.out.println("password:"+password);
		
		
		//System.out.println("custId: " + custId);
		//System.out.println("password: " + password);

		// model method호출
		boolean result = CustomerDAOImpl.getInstance().loginCheck(custId,
				password);

		HttpSession session = null;
		if (result) {
			
			int level=CustomerDAOImpl.getInstance().getCustomer(custId).getCustLevel();
			String custName=CustomerDAOImpl.getInstance().getCustomer(custId).getCustName();
			session = request.getSession();
			// 일정범위에 data저장
			session.setAttribute("custId", custId);//세선에 아이디저장
			session.setAttribute("level", level);
			session.setAttribute("custName", custName);
			
			response.setHeader("Cache-Control", "no-cache");
			
			
			
			//System.out.println("로그인 성공");
			ArrayList<Registeration> registerList = RegisterationDAOImpl
					.getInstance().getAllCourseId(custId);
			//System.out.println("tets" + registerList.size());

			request.setAttribute("registerList", registerList);

		} else {
			// nextPage.setViewName("index.jsp");
			System.out.println("로그인 실패");
		}

		request.setAttribute("loginResult", result);

		nextPage.setViewName("result.jsp");

		return nextPage;
	}

	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = null;

		HttpSession session = request.getSession(false);
		if (session != null)
			session.invalidate();// session소멸
		// nextPage.setViewName("index.jsp");
		// response.sendRedirect("index.jsp");
		RedirectView rv = new RedirectView("index.jsp");
		nextPage = new ModelAndView(rv);
		// nextPage.setView();
		return nextPage;
	}

		public ModelAndView showPermissionCust(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		ArrayList<Customer> custList=null;
		int selectedCustLevel = Integer.parseInt(request.getParameter("selectedCustLevel"));
		System.out.println("selectedCustLevel:"+selectedCustLevel);
		if(selectedCustLevel==0){
			System.out.println("전체보기");
			custList = CustomerDAOImpl.getInstance().AllPermissionCustList();}
		else{
			custList = CustomerDAOImpl.getInstance().permissionCustList(selectedCustLevel);
		}
		System.out.println("custList:"+custList);
		request.setAttribute("custList", custList);
		nextPage.setViewName("index.jsp?contentMenu=/admin/menuPage.jsp&content=/admin/custPermissionManagement.jsp");

		return nextPage;
	}
	
		public ModelAndView update_cust_admin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		
		String custId = request.getParameter("custId");
		String password= request.getParameter("password");
		int custLevel = Integer.parseInt(request.getParameter("custLevel"));
		String custEmail = request.getParameter("mailAccount") + "@"
				+ request.getParameter("mailDomain");
		String phoneNumber = request.getParameter("phoneNumber1")+request.getParameter("phoneNumber2")+request.getParameter("phoneNumber3");

		String parentPhoneNumber = request.getParameter("parentPhoneNumber1")+request.getParameter("parentPhoneNumber2")+request.getParameter("parentPhoneNumber3");
		
		
		String passwordQuestion = new String(request.getParameter("passwordQuestion").getBytes("8859_1"),"utf-8");
		String passwordAnswer= new String(request.getParameter("passwordAnswer").getBytes("8859_1"),"utf-8");
		
		System.out.println("custLevel: "+custLevel);
		Customer customer = new Customer(custId,password,custEmail,phoneNumber,parentPhoneNumber,passwordQuestion,passwordAnswer,custLevel);
		
		//System.out.println("update_cust_admin"+customer);	
		CustomerDAOImpl.getInstance().update_cust_admin(customer);
		request.setAttribute("custId", custId);
		request.setAttribute("custLevel", custLevel);
		request.setAttribute("parentPhoneNumber", parentPhoneNumber);

		ArrayList<Customer> custList = CustomerDAOImpl.getInstance()
				.getAllCustomers();
		request.setAttribute("custList", custList);
		nextPage.setViewName("index.jsp?contentMenu=/admin/menuPage.jsp&content=/admin/custList.jsp");
		return nextPage;
	}
		public ModelAndView update_custPermission(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();

		String custId = request.getParameter("custId");
		int custLevel = Integer.parseInt(request.getParameter("custLevel"));
		//String nextType=request.getParameter("nextType");
		
		//System.out.println("custId:"+custId+"custLevel:"+custLevel+"nextType:"+nextType);
		CustomerDAOImpl.getInstance().update_custPermission(custId,
				custLevel);
		//if(nextType.equals("noPermissionCust"))
		//	nextPage.setViewName("index.jsp?contentMenu=/admin/menuPage.jsp&content=/admin/noPermissionCustList.jsp");
		//else if(nextType.equals("permissionCust"))
		//	nextPage.setViewName("index.jsp?contentMenu=/admin/menuPage.jsp&content=/admin/permissionCustList.jsp");
		
		nextPage.setViewName("index.jsp?contentMenu=/admin/menuPage.jsp&content=showAllPermissionCust.cust");
		
		return nextPage;
	}
	
	public ModelAndView getCustomerList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		int custLevel = Integer.parseInt(request.getParameter("selectedCustLevel"));
		System.out.println("getCustomerList:"+custLevel);
		ArrayList<Customer> custList = CustomerDAOImpl.getInstance().getCustomerList(custLevel);
		
		request.setAttribute("custList", custList);
		nextPage.setViewName("index.jsp?contentMenu=/admin/menuPage.jsp&content=/admin/custList.jsp");
		return nextPage;
	}
	public ModelAndView showAllPermissionCust(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView nextPage = new ModelAndView();
		ArrayList<Customer> custList = CustomerDAOImpl.getInstance()
				.AllPermissionCustList();
		//System.out.println("Action : showAllPermissionCust");
		//System.out.println(custList);
		request.setAttribute("custList", custList);
		nextPage.setViewName("/admin/custPermissionManagement.jsp");
		return nextPage;
	}

	


	

	

	
	public ModelAndView idCheck(HttpServletRequest request,
			 HttpServletResponse response) throws Exception{
	
		ModelAndView nextPage=new ModelAndView();
		String custId=request.getParameter("custId");
		//System.out.println("custId: "+custId);
		if(custId==null || custId.equals(""))
			request.setAttribute("message","null");
		else{
			boolean result=CustomerDAOImpl.getInstance().loginIDCheck(custId);
			request.setAttribute("message", result);
			
		}
		
	
		nextPage.setViewName("/cust/result.jsp");//추후에 수정
		return nextPage;
	}
	
	private String getToday() {
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		Date d = new Date();
		String sdate = format.format(d);
		return sdate;
	}
}
